using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Threading;

using MULENetworking;
//using MainGame;

namespace MainGame
{
    public partial class PlayerCreation : Form
    {
        private string avatar = "";
        private string name;
        private IRemoteServer remoteServer;
        private IGame game;
        private ArrayList readyList;
        private int selected = -1;
        private Thread thread;

        public PlayerCreation(IRemoteServer server, string haxoralias, IGame gam)
        {
            InitializeComponent();
            remoteServer = server;
            name = haxoralias;
            game = gam;
            readyList = new ArrayList();
            thread = new Thread(new ThreadStart(PollingService));
            thread.Start();
        }


        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av2";
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av1";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av3";
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av4";
        }

        private void PlayerCreation_Load(object sender, EventArgs e)
        {

        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av6";
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av7";
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av8";
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            avatar = "av5";
        }

        private void ReadyButton_Click(object sender, EventArgs e)
        {
            if (name == null || avatar == null)
            {
                //do nothing;
            }
            else
            {
                //chatServer.CreatePlayer(name, avatar); DEPRECATED
                remoteServer.CreatePlayer(name, game);
                ReadyButton.Text = "Waiting for other players.";
                ReadyButton.Enabled = false;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FillListBox1()
        {
            int selected = listBox1.SelectedIndex;
            listBox1.Items.Clear();
            foreach (string playerName in readyList)
            {   
                listBox1.Items.Add(playerName);
            }
            if (listBox1.Items.Count > selected)
            {
                listBox1.SelectedIndex = selected;
            }
            if (listBox1.Items.Count == 4)
            {
                // start gameflow

                // start the game
                IPlayer thisPlayer = null;
                if(remoteServer.CurrentGame.Player1.name.Equals(name))
                {
                    thisPlayer = remoteServer.CurrentGame.Player1;
                }
                else if(remoteServer.CurrentGame.Player2.name.Equals(name))
                {
                    thisPlayer = remoteServer.CurrentGame.Player2;
                }
                else if(remoteServer.CurrentGame.Player3.name.Equals(name))
                {
                    thisPlayer = remoteServer.CurrentGame.Player3;
                }
                else if(remoteServer.CurrentGame.Player4.name.Equals(name))
                {
                    thisPlayer = remoteServer.CurrentGame.Player4;
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainGame(remoteServer, thisPlayer));
            }
        }

        private void PollingService()
        {
            ArrayList tempList = null;
            while (true)
            {
                Thread.Sleep(1000);

                tempList = remoteServer.PlayersList;
                if(readyList.Count == tempList.Count)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    readyList = tempList;

                    if (listBox1.InvokeRequired)
                    {
                        try
                        {
                            listBox1.Invoke(new MethodInvoker(FillListBox1), null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.InnerException.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Polling service is running in the UI thread for some reason...");
                        Console.WriteLine("listBox1 did not require an invoke, and was not updated.");
                    }
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (thread != null)
            {
                thread.Abort();
            }
        }
    }
}